package com.learn.cloud.validator.config;


import com.learn.cloud.validator.code.ValidationProxyProcessor;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import org.hibernate.validator.HibernateValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;

/**
 * @description: 校验自动配置
 * @author: cong zhi
 * @createDate: 2022/2/6 1:30
 * @updateUser: cong zhi
 * @updateDate: 2022/2/6 1:30
 * @updateRemark: 修改内容
 * @version: v1.0
 */
@Configuration
public class ValidatorPlusAutoConfiguration {


    @Bean
    public ValidationProxyProcessor validationProxyProcessor() {
        return new ValidationProxyProcessor();
    }

    @Bean
    public MethodValidationPostProcessor methodValidationPostProcessor() {
        MethodValidationPostProcessor postProcessor = new MethodValidationPostProcessor();
        postProcessor.setValidator(validator());
        return postProcessor;
    }
    /**
     *  配置验证程序
     * <p>https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/</p>
     * @return
     */
    @Bean
    public Validator validator() {
        ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class)
                .configure()
                // 将fail_fast设置为true即可，如果想验证全部，则设置为false或者取消配置即可
                .failFast(true)
                .buildValidatorFactory();
        Validator validator = validatorFactory.getValidator();
        return validator;
    }
}
